package com.wx.wxapp.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.wxapp.dao.*;
import com.wx.wxapp.entity.*;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DiseaseService {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /*
     *   功能：获取用户关于疾病的发帖纪录
     *   参数：
     *       openid:用户的openid
     * */
    @Transactional
    public List<List<String>> getHistoryDiseaseCard(String openid){
        List<List<String>> diseaseList = new ArrayList<>();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DiseaseMapper diseaseMapper = sqlSession.getMapper(DiseaseMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        DiseaseimageMapper diseaseimageMapper = sqlSession.getMapper(DiseaseimageMapper.class);
        User user = userMapper.selectByOpenid(openid);
        int id = user.getId();
        List<Disease> diseaseCards = diseaseMapper.selectByUserId(id);
        Iterator<Disease> it1 = diseaseCards.iterator();
        DiseaseCard(diseaseList,diseaseimageMapper,it1);
        sqlSession.close();
        return diseaseList;
    }
    @Transactional
    public Map getDiseaseCard(int pageNumber) {
        Map map = new HashMap();
        List<List<String>> diseaseList = new ArrayList<>();
        PageHelper.startPage(pageNumber,4);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DiseaseMapper diseaseMapper = sqlSession.getMapper(DiseaseMapper.class);
        DiseaseimageMapper diseaseimageMapper = sqlSession.getMapper(DiseaseimageMapper.class);
        List<Disease> diseaseCards = diseaseMapper.selectAll();
        PageInfo<Disease> pageInfo = new PageInfo<>(diseaseCards);
        int size = pageInfo.getPages();
        Iterator<Disease> it1 = diseaseCards.iterator();
        DiseaseCard(diseaseList,diseaseimageMapper,it1);
        sqlSession.close();
        map.put("list",diseaseList);
        map.put("size",size);
        return map;
    }

    public void DiseaseCard(List<List<String>> diseaseList, DiseaseimageMapper diseaseimageMapper, Iterator<Disease> it1) {
        while (it1.hasNext()){
            List<String> list = new ArrayList<>();
            Disease disease = it1.next();
            list.add(disease.getId());list.add(disease.getTitle());list.add(disease.getInformation());list.add(disease.getUsername());list.add(disease.getImage());list.add((disease.getPraisenumber().toString()));list.add(disease.getLabel());
            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(disease.getTime());
            list.add(date);
            String cardId = disease.getId();
            List<Diseaseimage> diseaseimages = diseaseimageMapper.selectByCardId(cardId);
            Iterator<Diseaseimage> it2 = diseaseimages.iterator();
            while (it2.hasNext()){
                list.add(it2.next().getImagename());
            }
            diseaseList.add(list);
        }
    }
    @Transactional
    public Map GetCardsByDiseaseLabel(String label){
        System.out.println("label"+label);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DiseaseMapper diseaseMapper = sqlSession.getMapper(DiseaseMapper.class);
        List<Disease> diseases = diseaseMapper.selectByLabel(label);
        System.out.println(diseases);
        return null;
    }
}
